<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Service extends MY_Controller {
	function __construct() {
		parent::__construct();

		$this->load->model('type_model', '', TRUE);
		$this->load->model('con_common_model', '', TRUE);
		$this->load->model('con_photo_model', '', TRUE);
		$this->load->model('con_team_model', '', TRUE);
		$this->load->model('position_model', '', TRUE);
		$this->load->model('region_model', '', TRUE);
	}
	
	public function common() {
		if ($this->input->is_ajax_request()) {
			$type = $this->input->post("type");
			$selector = $this->input->post("selector");
			$operate = $this->input->post("operate");
			$is_submit = ($this->input->post("is_submit") == 'false') ? FALSE : TRUE;
			$form_data['textarea'] = $this->input->post("form_textarea_data");
			
			if (!$is_submit) {
				$data = $this->detail($type, $selector, $operate);;
				
				$data['selector'] = $selector;
				$data['operate'] = $operate;
				
				$data['selector_data'] = $this->_get_current_editable_obj($type, $selector);
				
				$this->load->view('backend/' . $this->parent_type . '/common', $data);
			} else {
				$data = $this->detail($type, $selector, $operate, true, $form_data);
				
				if (!empty($data['res'])) {
					$this->output->set_output(json_encode($data['res']));
				} else {
					$this->output->set_output(json_encode('success'));
				}
			}
		}
	}
	
	public function detail($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('#intro #content'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					$data['intro'] = $this->con_common_model->search_by_condition($condition, TRUE);
				} else {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					$intro = $this->con_common_model->search_by_condition($condition, TRUE);
					
					$update['id'] = $intro['id'];
					$update['content'] = $form_data['textarea'];
					$this->con_common_model->update($update);
				}
				break;
			case selector_replace('#service_carousel .viewport'):
				if ($operate == 'create') {
					if (!$is_submit) {
						
					} else {
						$editable_obj = $this->_get_current_editable_obj($type, $selector);
						$upload_data = $this->_upload_pic();
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save photo
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data['photo_path'] = $upload_data['file_db_path'];
						$save_data['upload_time'] = time();
						
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_photo_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						
						
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
					} else {
						$this->_update_weight($this->input->post("pic_weight"), 'photo');
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_photo_model->update($update_data);
					}
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
}